// 权限控制
import router from './router'
import store from '@/store'
import { getToken } from '@/utils/auth'
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'

const WHITE_LIST = ['/login', '/404'] // 白名单

router.beforeEach((to, from, next) => {
  NProgress.start()
  const token = getToken()
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
      if (!store.state.user.userInfo.userId) {
        store.dispatch('user/fetchUserInfo')
      }
    }
  } else {
    if (WHITE_LIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  NProgress.start()
})
